﻿using System;
using Microsoft.Xna.Framework;
using SolarWinds.Helpers;

namespace SolarWinds.Particles
{
    class CircleParticleEmitter : ParticleEmitter
    {
        public CircleParticleEmitter(ParticleEmitterDescription description) : base(description)
        {
        }

        protected override void InitParticle(ref Particle p)
        {
            // random point inside circle
            float randomAngle = RandomHelper.RandomFloat2() * 2.0f * MathHelper.Pi;
            float randomRadius = RandomHelper.RandomFloat2() * mDescription.Radius;

            var randPoint = new Vector3(randomRadius * (float)Math.Cos(randomAngle),
                                         0,
                                         randomRadius * (float)Math.Sin(randomAngle));

            // set position and velocity
            p.PreviousPosition = mDescription.Position + randPoint;
            p.Position = p.PreviousPosition;
            p.Velocity = mDescription.Direction * mDescription.Speed;

            // spread particle direction by rotation of velocity vector
            float spreadRad = MathHelper.ToRadians(mDescription.Spread);
            p.Velocity = Vector3.Transform(p.Velocity, Matrix.CreateRotationX(spreadRad * RandomHelper.RandomFloat2()));
            p.Velocity = Vector3.Transform(p.Velocity, Matrix.CreateRotationY(spreadRad * RandomHelper.RandomFloat2()));
            p.Velocity = Vector3.Transform(p.Velocity, Matrix.CreateRotationZ(spreadRad * RandomHelper.RandomFloat2()));
        }
    }
}
